package cc.orange.http;

/**
 * Created by CM on 2021/3/1.
 */

import com.xcheng.retrofit.Call;
import com.xcheng.retrofit.CallAdapterFactory;
import com.xcheng.retrofit.RetrofitFactory;

import java.security.cert.CertificateException;
import java.util.concurrent.TimeUnit;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;


import cc.orange.entity.BaseEntity;
import cc.orange.entity.ChanListEntity;
import cc.orange.entity.ChatAnswerEntity;
import cc.orange.entity.FriendItemInfo;
import cc.orange.entity.FriendItemInfoOne;
import cc.orange.entity.GradeInfo;
import cc.orange.entity.KFChatListEntity;
import cc.orange.entity.LoginsEntity;
import cc.orange.entity.MsgInfosVideoEntity;
import cc.orange.entity.MsgSetEntity;
import cc.orange.entity.NoticeEntity;
import cc.orange.entity.PersonContentEntity;
import cc.orange.entity.ReturnImageEntity;
import cc.orange.entity.ReturnWmEntity;
import cc.orange.entity.UserInfoEntity;
import cc.orange.utils.NetConstant;
import cc.orange.entity.AllGradeInfo;
import cc.orange.entity.ChatHistoryEntity;
import cc.orange.entity.MainPopuDataEntity;
import cc.orange.entity.PersonMsgEntity;
import okhttp3.OkHttpClient;
import okhttp3.RequestBody;
import retrofit2.Retrofit;
import retrofit2.http.Body;
import retrofit2.http.GET;
import retrofit2.http.Header;
import retrofit2.http.POST;
import retrofit2.http.Path;
import retrofit2.http.Query;

public interface ApiService {
    String HOST = "http://60.205.204.159:8084";
//    String HOST = "http://192.168.31.192:8084";
   static void init() {

        DownLoadInterceptor interceptor = new DownLoadInterceptor();
        interceptor.setLevel(DownLoadInterceptor.Level.BODY);

        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(HOST)
                .client(getUnsafeOkHttpClient())
                .callFactory(new OkHttpClient.Builder()
                        .addNetworkInterceptor(interceptor)
                        .connectTimeout(60, TimeUnit.SECONDS)
                        .callTimeout(60,TimeUnit.SECONDS)
                        .readTimeout(60,TimeUnit.SECONDS)
                        .writeTimeout(60,TimeUnit.SECONDS)
                        .build())
                //必须添加此adapter 用于构建Call
                .addCallAdapterFactory(CallAdapterFactory.INSTANCE)
                //添加自定义json解析器
                .addConverterFactory(MyGsonConverterFactory.create())
                .build();
        RetrofitFactory.DEFAULT = retrofit;
    }

    public static OkHttpClient getUnsafeOkHttpClient() {
        try {
            // Create a trust manager that does not validate certificate chains
            final TrustManager[] trustAllCerts = new TrustManager[] {
                    new X509TrustManager() {
                        @Override
                        public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
                        }

                        @Override
                        public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
                        }

                        @Override
                        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                            return new java.security.cert.X509Certificate[]{};
                        }
                    }
            };

            // Install the all-trusting trust manager
            final SSLContext sslContext = SSLContext.getInstance("SSL");
            sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
            // Create an ssl socket factory with our all-trusting manager
            final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();

            OkHttpClient.Builder okHttpBuilder = new OkHttpClient.Builder()
                    .connectTimeout(NetConstant.API_CONNECT_TIME_OUT, TimeUnit.SECONDS)
                    .readTimeout(NetConstant.API_READ_TIME_OUT, TimeUnit.SECONDS)
                    .callTimeout(NetConstant.API_READ_TIME_OUT, TimeUnit.SECONDS)
                    .writeTimeout(NetConstant.API_WRITE_TIME_OUT, TimeUnit.SECONDS);
            okHttpBuilder.sslSocketFactory(sslSocketFactory);
            okHttpBuilder.hostnameVerifier(new HostnameVerifier() {
                @Override
                public boolean verify(String hostname, SSLSession session) {
                    return true;
                }
            });

            return okHttpBuilder.build();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /*

     */
    @POST(HOST + "/api/login")
    Call<LoginsEntity> login(@Body RequestBody request);


    @GET(HOST + "/api/default/returnPerson")
    Call<MainPopuDataEntity> returnPerson(@Header ("Authorization")String heads);

//    返回特定用户的信息--用户基本信息+评价    请求参数 id(假用户id)
    @GET(HOST + "/api/default/returnOne")
    Call<PersonMsgEntity> returnOne(@Header ("Authorization")String heads, @Query("id") String id);


//    返回选中用户关联的动态     请求参数 id(假用户id)
    @GET(HOST + "/api/default/returnDongTai")
    Call<PersonContentEntity> returnDongTai(@Header ("Authorization")String heads, @Query("id") String id);

    //返回所有动态
    @GET(HOST + "/api/default/dynamicAll")
    Call<PersonContentEntity> dynamicAll(@Header ("Authorization")String heads);


//    返回选中用户关联的视频   /api/default/returnVideo  请求参数 id(假用户id)
    @GET(HOST + "/api/default/returnVideo")
    Call<MsgInfosVideoEntity> returnVideo(@Header ("Authorization")String heads, @Query("id") String id);

//    用户点赞接口   did为动态id  uid女用户id flag为点赞标识 1不点赞 0点赞
    @GET(HOST + "/api/default/getLike/{dId}/{uId}/{flag}")
    Call<PersonContentEntity> defaultgetLike(@Header ("Authorization")String heads,
                                             @Path("dId") String dId,
                                             @Path("uId") String uId,
                                             @Path("flag") String flag);


    //    用户关注接口
    @GET(HOST + "/api/default/attention/{uId}/{flag}")
    Call<BaseEntity> attention(@Header ("Authorization")String heads,
                               @Path("uId") String uId,
                               @Path("flag") String flag);

    //    用户评价接口
    @GET(HOST + "/api/default/evaluate/{dId}/{uId}/{flag}")
    Call<BaseEntity> evaluate(@Header ("Authorization")String heads,
                                            @Path("dId") String dId,
                                             @Path("uId") String uId,
                                             @Path("flag") String flag);

    //    拉黑 block按钮
    @GET(HOST + "/api/default/block/{uId}/{flag}")
    Call<BaseEntity> block(@Header ("Authorization")String heads,
                               @Path("uId") String uId,
                               @Path("flag") String flag);

    // 返回相关设置
    @GET(HOST + "/api/default/setting/{uId}")
    Call<MsgSetEntity> setting(@Header ("Authorization")String heads,
                               @Path("uId") String uId);

    // 不看Ta的动态 noWatch 按钮
    @GET(HOST + "/api/default/noWatch/{uId}/{flag}")
    Call<BaseEntity> noWatch(@Header ("Authorization")String heads,
                             @Path("uId") String uId,
                             @Path("flag") String flag);


//    // 保存聊天记录
    @POST(HOST + "/api/chat/saveChat")
    Call<BaseEntity> saveChat(@Header ("Authorization")String heads,
                              @Query("womanId") String womanId,
                              @Query("chat") String chat,
                              @Query("packageName") String packageName,
                              @Query("channel") String channel,
                              @Query("version") String version);


    // 查找聊天记录
    @GET(HOST + "/api/chat/findChat/{uId}")
    Call<ChatHistoryEntity> findChat(@Header ("Authorization")String heads,
                                     @Path("uId") String uId);
    // 查找聊天记录
    @GET(HOST + "/api/chat/findChat/{uId}")
    Call<ChatHistoryEntity> findChat(@Header ("Authorization")String heads,
                                     @Path("uId") String uId, @Query("pageName") String pageName);

    // 查找用户聊天列表
    @GET(HOST + "/api/chat/chatList")
    Call<ChanListEntity> chatList(@Header ("Authorization")String heads, @Query("pageName") String pageName);

    // 自动回复聊天记录
    @GET(HOST + "/api/chat/chat/{uId}")
    Call<ChatAnswerEntity> chatAnswer(@Header ("Authorization")String heads,
                                      @Path("uId") String uId);


    //通过token获取用户信息
    @GET(HOST + "/api/user/getSuserInfo")
    Call<UserInfoEntity> getSuserInfo(@Header("Authorization") String heads);

      //     /api/video/returnVideo  //返回全部视频
    @GET(HOST + "/api/video/returnVideo")
    Call<MsgInfosVideoEntity> returnVideoF(@Header ("Authorization")String heads);



//    // 首页视频分类 首页视频分类 *3-热门
//    //1-活跃
//    //2-新人
//    @GET(HOST + "/api/video/homeVideo/{classId}")
//    Call<MsgInfosVideoEntity> homeVideo(@Header ("Authorization")String heads,
//                               @Path("classId") String classId);

    // 首页视频分类 首页视频分类 *3-热门
    //1-活跃
    //2-新人 /api/video/homeVideo/3
    @GET(HOST + "/api/noLogin/homeVideo")
    Call<MsgInfosVideoEntity> homeVideo(@Header ("Authorization")String heads
            ,@Query("flag") String flag,@Query("userId") String userId);


    // 视频点赞
    @GET(HOST + "/api/default/likeVideo/{videoId}/{uId}/{flag}")
    Call<BaseEntity> likeVideo(@Header ("Authorization")String heads,
                               @Path("videoId") String videoId,
                               @Path("uId") String uId,
                               @Path("flag") String flag);


    // 举报
    @POST(HOST + "/api/default/report")
    Call<BaseEntity> report(@Header ("Authorization")String heads,
                            @Body RequestBody request);


    // 用户发送客服信息
    @GET(HOST + "/api/chat/returnKefu")
    Call<BaseEntity> returnKefu(@Header ("Authorization")String heads,
                               @Query("text") String text);

    // 获取客服聊天记录
    @GET(HOST + "/api/chat/getKeFuChat")
    Call<KFChatListEntity> getKeFuChat(@Header ("Authorization")String heads);


    //  获取我的等级
    @GET(HOST + "/api/default/myLevel")
    Call<GradeInfo> myLevel(@Header("Authorization") String heads);

    // 获取等级列表
    @GET(HOST + "/api/levelAll/all")
    Call<AllGradeInfo> levelAll(@Header("Authorization") String heads);



    // 公告接口
    @GET(HOST + "/api/noLogin/notice")
    Call<NoticeEntity> notice();

    // 获取聊友列表
    @GET(HOST + "/api/user/friendList")
    Call<FriendItemInfo> getFriendList(@Header("Authorization") String heads);

    // 获取粉丝列表
    @GET(HOST + "/api/user/fansList")
    Call<FriendItemInfo> getFansList(@Header("Authorization") String heads);

    // 获取粉丝列表
    @GET(HOST + "/api/user/followerList")
    Call<FriendItemInfo> getFollowerList(@Header("Authorization") String heads);

//     获取推荐列表
    @GET(HOST + "/api/user/returnFirend")
    Call<FriendItemInfoOne> getReturnFriend(@Header("Authorization") String heads);

//     获取速配列表
    @GET(HOST + "/api/noLogin/returnHomeVideo")
    Call<MsgInfosVideoEntity> getSpeedVideo(@Header ("Authorization")String heads,
                                            @Query("num") String num, @Query("userId") String userid);

    // 公告接口
    @GET(HOST + "/api/user/deleteUser")
    Call<BaseEntity> deleteUser(@Header("Authorization") String heads);

    // 发布内容
    @POST(HOST + "/api/user/insertDynamic")
    Call<BaseEntity> insertDynamic(@Header ("Authorization")String heads,
                            @Body RequestBody request);


    // yonghuxinxi
    @GET(HOST + "/api/default/returnWoMan")
    Call<ReturnWmEntity> returnWoMan(@Header ("Authorization")String heads, @Query("id") String id);


    // 图片列表
    @GET(HOST + "/api/default/returnImag")
    Call<ReturnImageEntity> returnImag(@Header ("Authorization")String heads, @Query("id") String id);
}



